﻿using System;
using System.Drawing;
using Tesseract;

namespace TesseractOcr
{
    public class TesseractOcr
    {
        public static int MeanConfidence { get; set; }

        public static string StartOcr(string filename)
        {
            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                //engine.SetVariable("tessedit_char_whitelist", "0123456789");
                // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                using (var image = new Bitmap(filename))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            MeanConfidence = System.Convert.ToInt32(page.GetMeanConfidence() * 100);
                            return page.GetText();
                        }
                    }
                }
            }
        }
    }
}
